<?php

/**
 * uniBoard ACP Styles page
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-08-15 04:25:27 +0200 (N, 15 sie 2010) $
 * $Revision: 61 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class action_styles{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore		= NULL;
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Action
	 *
	 * @param object $appCore
	 */
	
	function __construct( $appCore){
		
		// Store appCore pointer
		$this -> appCore = $appCore;
		
		// Set page help
		$appCore -> output -> page_help = $appCore -> lang['acp_styles_list_help'];
		
		// Load fields templates
		$appCore -> templates['form_inputs'] = new tpl_form_inputs( $this -> appCore);
		
		// Cache output
		ob_start();
		
		// Open table block
		echo $appCore -> output -> openTableBlock();
		
		// Draw header
		echo $appCore -> output -> tableHeader( array(
			array(
				'left_float width_quarter',
				$appCore -> lang['styles_list_style']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['actions']
			),
			array(
				'right_float width_medium',
				$appCore -> lang['styles_list_users']
			)
		));
		
		// Styles list (un-ondered)
		$styles_list = array();
		
		// Grab em and add em!
		$appCore -> db -> query( 'SELECT s.style_id, s.style_parent, s.style_name, COUNT( u.user_id) AS style_users
		FROM ' . DB_PREFIX . 'styles s
		LEFT JOIN ' . DB_PREFIX . 'users u ON  s.style_id = u.user_style
		GROUP BY s.style_id
		ORDER BY s.style_parent, s.style_name');
		
		// Parse result
		while ( $style_result = $appCore -> db -> fetch_assoc())
		{
			// Create empty parent array
			if ( !isset( $styles_list[$style_result['style_parent']]))
			{
				$styles_list[$style_result['style_parent']] = array();
			}
			
			// Add style to array
			$styles_list[$style_result['style_parent']][$style_result['style_id']] = $style_result;
		}
		
		// Free result
		$appCore -> db -> freeResult();
		
		// Order styles
		$styles_list = class_hierarchy::doOrdering( $styles_list, array(
			$appCore -> output -> drawImage( 'tree_s'),
			$appCore -> output -> drawImage( 'tree_i'),
			$appCore -> output -> drawImage( 'tree_t'),
			$appCore -> output -> drawImage( 'tree_l')
		));
		
		// Make style default?
		if ( $appCore -> _REQ( 'do') == 'default')
		{
			// New style id
			$new_def = (integer) $appCore -> _REQ( 'style');
			
			// Style exists and is not default?
			if ( $new_def > 1 && isset( $styles_list[$new_def]))
			{
				// Update board config
				$appCore -> setConfig( 'default_style', $new_def);	
				
				// Make Guest use this lang
				$appCore -> db -> update( array( 'user_style' => $new_def), 'users', 'user_id = \'-1\'');
				
				// Create Log
				$appCore -> logAdmin( 'log_admin_default_style', htmlspecialchars( $styles_list[$new_def]['style_name']));
				
				// Show message
				$appCore -> output -> message( $appCore -> lang['acp_style_default_updated'], $appCore -> lang['acp_styles_list']);
			}
		}
		
		// Reiterate items
		foreach ( $styles_list as $style_data)
		{
			// Style images
			if ( $style_data['style_parent'] == 0)
			{
				// Style image
				$style_image = $appCore -> output -> drawImage( 'style_root', $appCore -> lang['style_type_root']);
				
				// Style status
				$style_status = $appCore -> output -> drawImage( 'item_not_default', $appCore -> lang['style_cant_default']);
			}
			else if ( $style_data['style_parent'] == 1)
			{
				// Style image
				$style_image = $appCore -> output -> drawImage( 'style_normal', $appCore -> lang['style_type_normal']);
				
				// Style status
				$style_status = ( $style_data['style_id'] == $appCore -> conf['default_style'] ? $appCore -> output -> drawImage( 'item_default', $appCore -> lang['style_is_default']) : '<a href="' . $appCore -> adminLink( 'act=styles&do=default&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'item_not_default', $appCore -> lang['style_make_default']) . '</a>');
			}
			else
			{
				// Style image
				$style_image = $appCore -> output -> drawImage( 'style_child', $appCore -> lang['style_type_child']);
				
				// Style status
				$style_status = ( $style_data['style_id'] == $appCore -> conf['default_style'] ? $appCore -> output -> drawImage( 'item_default', $appCore -> lang['style_is_default']) : '<a href="' . $appCore -> adminLink( 'act=styles&do=default&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'item_not_default', $appCore -> lang['style_make_default']) . '</a>');
			}

			// Style actions
			if ( $style_data['style_parent'] == 0)
			{
				// Dev tools?
				if ( DEVELOPER_MODE)
				{
					$style_actions = '<a href="' . $appCore -> adminLink( 'act=edit_style&do=details&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_edit', $appCore -> lang['style_op_edit']) . ' ' . $appCore -> lang['style_op_edit'] . '</a>
					<a href="' . $appCore -> adminLink( 'act=edit_style&do=css&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_style_css', $appCore -> lang['style_op_css']) . ' ' . $appCore -> lang['style_op_css'] . '</a>
					<a href="' . $appCore -> adminLink( 'act=edit_style&do=js&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_style_js', $appCore -> lang['style_op_js']) . ' ' . $appCore -> lang['style_op_js'] . '</a>
					<a href="' . $appCore -> adminLink( 'act=templates&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_templates', $appCore -> lang['style_op_templates']) . ' ' . $appCore -> lang['style_op_templates'] . '</a>';
				}
				else
				{
					$style_actions = '<div>' . $appCore -> lang['style_noacces_master'] . '</div>';
				}
			}
			else
			{
				$style_actions = '<a href="' . $appCore -> adminLink( 'act=edit_style&do=details&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_edit', $appCore -> lang['style_op_edit']) . ' ' . $appCore -> lang['style_op_edit'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_style&do=css&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_style_css', $appCore -> lang['style_op_css']) . ' ' . $appCore -> lang['style_op_css'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_style&do=js&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_style_js', $appCore -> lang['style_op_js']) . ' ' . $appCore -> lang['style_op_js'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=templates&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_templates', $appCore -> lang['style_op_templates']) . ' ' . $appCore -> lang['style_op_templates'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_style&do=resynch&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_refresh', $appCore -> lang['style_op_recompile']) . ' ' . $appCore -> lang['style_op_recompile'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_style&do=users&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_users', $appCore -> lang['style_op_users']) . ' ' . $appCore -> lang['style_op_users'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_style&do=copy&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_copy', $appCore -> lang['style_op_make_chidren']) . ' ' . $appCore -> lang['style_op_make_chidren'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_style&do=export&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_download', $appCore -> lang['style_op_export']) . ' ' . $appCore -> lang['style_op_export'] . '</a>
				<a href="' . $appCore -> adminLink( 'act=edit_style&do=delete&style=' . $style_data['style_id']) . '">' . $appCore -> output -> drawImage( 'action_delete', $appCore -> lang['style_op_delete']) . ' ' . $appCore -> lang['style_op_delete'] . '</a>';
			}
			
			// Draw row
			echo $appCore -> output -> tableRow('
				<div class="cell left_float width_longer">
					' . $style_data['item_image'] . $style_image . '<b> ' . htmlspecialchars( $style_data['style_name']) . '</b>
				</div>
				<div class="cell left_border right_float width_medium">
					' . $appCore -> output -> toolsPopup( $style_actions) . '
				</div>
				<div class="cell left_border right_float width_medium center">
					' . ( $style_data['style_id'] == $appCore -> conf['default_style'] ? $appCore -> number( $style_data['style_users']) - 1 : $appCore -> number( $style_data['style_users'])) . '
				</div>
				<div class="cell left_border right_float width_tiny center">
					' . $style_status . '
				</div>');
		}
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
		
		// New style form start
		echo $appCore -> output -> openForm( $appCore -> adminLink( 'act=new_style', true));
		echo $appCore -> drawHidden( 'update', 1);
		echo $appCore -> output -> openFormBlock( $appCore -> lang['new_style_form']);
		
		// Draw field
		echo $appCore -> output -> formItem( $appCore -> lang['new_style_form_name'], $appCore -> drawTextInput( 'new_style_name'), $appCore -> lang['new_style_form_name_expl']);
		
		// New style form close
		echo $appCore -> output -> submitButton( $appCore -> lang['new_style_form_submit']);
		echo $appCore -> output -> closeFormBlock();
		echo $appCore -> output -> closeForm();
		
		// Open table block
		echo $appCore -> output -> openTableBlock( $appCore -> lang['styles_list_legend']);
		
		// Draw 3 legend rows
		echo $appCore -> output -> tableRow( '<div class="cell left_float width_tiny center">
					' . $appCore -> output -> drawImage( 'style_root', $appCore -> lang['style_type_root']) . '
				</div>
				<div class="cell left_border left_float width_hudge">
					<b>' . $appCore -> lang['style_type_root'] . '</b>
					<p>' . $appCore -> lang['style_type_root_expl'] . '</p>
				</div>');
		
		echo $appCore -> output -> tableRow( '<div class="cell left_float width_tiny center">
					' . $appCore -> output -> drawImage( 'style_normal', $appCore -> lang['style_type_normal']) . '
				</div>
				<div class="cell left_border left_float width_hudge">
					<b>' . $appCore -> lang['style_type_normal'] . '</b>
					<p>' . $appCore -> lang['style_type_normal_expl'] . '</p>
				</div>');
		
		echo $appCore -> output -> tableRow( '<div class="cell left_float width_tiny center">
					' . $appCore -> output -> drawImage( 'style_child', $appCore -> lang['style_type_child']) . '
				</div>
				<div class="cell left_border left_float width_hudge">
					<b>' . $appCore -> lang['style_type_child'] . '</b>
					<p>' . $appCore -> lang['style_type_child_expl'] . '</p>
				</div>');
		
		echo $appCore -> output -> tableRow( '<div class="cell left_float width_tiny center">
					' . $appCore -> output -> drawImage( 'item_default', $appCore -> lang['style_is_default']) . '
				</div>
				<div class="cell left_border left_float width_hudge">
					<b>' . $appCore -> lang['style_is_default'] . '</b>
					<p>' . $appCore -> lang['style_is_default_expl'] . '</p>
				</div>');
		
		// Close table block
		echo $appCore -> output -> closeTableBlock();
		
		// Draw output
		$appCore -> output -> draw( $appCore -> lang['acp_styles_list'], array( $appCore -> adminLink( 'act=styles') => $appCore -> lang['acp_styles_list']), ob_get_clean());
				
	}

}